<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>部门列表</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新增
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="expand"> 展开 </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="fold"> 折叠 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-update" lay-event="update">
                <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
            </a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delFlag">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </a>
        </script>
    </div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'table', 'treeTable'], function () {
        var form = layui.form,
            treeTable = layui.treeTable;

        var isShow = true;

        var insTb = treeTable.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'deptinfo/list',
            skin: 'line',
            tree: {
                treeColIndex: 1,
                treeSpid: "0",
                iconIndex: 1,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'deptId',  // id字段名称
                pidName: 'parentId',     // pid字段名称
                arrowType: 'arrow2',
                getIcon: 'ew-tree-icon-style2'
            },
            treeDefaultClose: isShow,
            method : 'GET',
            page: false,
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'deptName', minWidth: 200, title: '部门名称'},
                    {field: 'leader', title: '负责人', align: 'center'},
                    {field: 'phone', title: '联系电话', align: 'center'},
                    {field: 'email', title: '邮箱', align: 'center',},
                    {field: 'orderNum', title: '排序', align: 'center', edit: 'number'},
                    {field: 'status', title: '状态', align: 'center', templet: function (d) {
                            if (0 == d.status) {
                                return '<span class="layui-badge layui-bg-blue">正常</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-orange">停用</span>';
                            }
                        }
                    },
                    {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
                ]
            ]
        });


        // 监听单元格编辑
        treeTable.on('edit(currentTableId)', function(obj){
            var value = obj.value
                ,data = obj.data;
            if(!/^\+?[0-9][0-9]*$/.test(value)){
                Message.warning('请输入数字，且大于0!', 1000);
                return false;
            }
            var index = layer.load(2, {shade: 0.1});
            AjaxUtil.get({
                url: AjaxUtil.ctx + 'deptinfo/updateSort/' + data.deptId + '/' + value,
                success: function (res) {
                    layer.close(index);
                    if (0 === res.code) {
                        layer.closeAll('dialog');
                        Message.success(500, res.message, function () {
                            // 重载表格
                            insTb.refresh();
                        });
                    } else {
                        Message.error(res.message, 1000);
                    }
                },
                error: function (error) {
                    layer.close(index);
                    Message.error('服务器异常！', 1000);
                }
            });
        });

        treeTable.on('toolbar(currentTableId)', function (obj) {
            if (obj.event === 'add') {
                var index = layer.open({
                    title: '新增部门',
                    type: 2,
                    maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'deptinfo/forAdd',
                    end: function(index){
                        insTb.refresh();
                    }
                });
                return false;
            } else if (obj.event === 'expand') {
                isShow = true;
                insTb.expandAll();// 展开全部
            } else if (obj.event === 'fold') {
                isShow = false;
                insTb.foldAll();// 折叠全部
            }
        });

        treeTable.on('tool(currentTableId)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                layer.open({
                    type: 2,
                    shade: 0.2,
                    title: '修改部门',
                    maxmin: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'deptinfo/forUpdate/' + data.deptId,
                    end: function(index) {
                        insTb.refresh();
                    }
                });
                return false;
            } else if (obj.event === 'delFlag') {
                layer.confirm('确认删除该数据以及所有子级数据', {
                    title: '提示'
                }, function(){
                    var index = layer.load(0, {shade: 0.1});
                    AjaxUtil.get({
                        url: AjaxUtil.ctx + 'deptinfo/delete/' + data.deptId,
                        success: function (res) {
                            layer.close(index);
                            if (0 === res.code) {
                                layer.closeAll('dialog');
                                Message.success(500, res.message, function () {
                                    // 重载表格
                                    insTb.refresh();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                            layer.close(index);
                            Message.error("服务器异常！", 1000);
                        }
                    });
                });
                return false;
            }
        });
    });
</script>

</body>
</html>